package rbac

import (
	"github.com/gin-gonic/gin"
	"store/libs/helper"
	"store/libs/log"
	"store/models"
	"store/models/base"
)

const (
	TABLE_ADMIN_RIGHTS = "admin_rights"
)

type AdminRights struct {
	Name        string `json:"name" xml:"name"`
	Level       uint8  `json:"level" xml:"level"`
	ParentId    uint64 `json:"parent_id" xml:"parent_id"`
	IsOn        uint8  `json:"is_on" xml:"is_on"`
	AddTime     string `json:"add_time" xml:"add_time"`
	UpdateTime  uint64 `json:"update_time" xml:"update_time"`
	Id          uint64 `json:"id" xml:"id"`
	Tag         string `json:"tag" xml:"tag"`
	Description string `json:"description" xml:"description"`
	Range       uint8  `json:"range" xml:"range"`
	IsShow      uint8  `json:"is_show" xml:"is_show"`
	Version     uint64 `json:"version" xml:"version"`
}

type SortRights struct {
	list   []AdminRights
	sortBy func(right1, right2 *AdminRights) bool
}

func NewSortRights(l []AdminRights, by func(right1, right2 *AdminRights) bool) *SortRights {
	return &SortRights{
		list:   l,
		sortBy: by,
	}
}

func (sr *SortRights) Len() int {
	return len(sr.list)
}

func (sr *SortRights) Swap(i, j int) {
	sr.list[i], sr.list[j] = sr.list[j], sr.list[i]
}

func (sr *SortRights) Less(i, j int) bool {
	return sr.sortBy(&sr.list[i], &sr.list[j])
}

func ByLevel(right1, right2 *AdminRights) bool {
	if right1.Level == right2.Level {
		return right1.Id < right2.Id
	}

	return right1.Level < right2.Level
}

/**
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminRightsMap2Model(data map[string]string) AdminRights {
	return AdminRights{
		Range:       uint8(helper.ToInt(data["range"])),
		IsShow:      uint8(helper.ToInt(data["is_show"])),
		Version:     uint64(helper.ToInt(data["version"])),
		Id:          uint64(helper.ToInt(data["id"])),
		Tag:         data["tag"],
		Description: data["description"],
		IsOn:        uint8(helper.ToInt(data["is_on"])),
		AddTime:     data["add_time"],
		UpdateTime:  uint64(helper.ToInt(data["update_time"])),
		Name:        data["name"],
		Level:       uint8(helper.ToInt(data["level"])),
		ParentId:    uint64(helper.ToInt(data["parent_id"])),
	}
}

/**
 * 获取所有启用权限
 */
func AllEnableRights(ctx *gin.Context) []AdminRights {
	rightsList := []AdminRights{}

	rows, err := models.Orm.Table(TABLE_ADMIN_RIGHTS).Where("is_on = 1").Rows()
	if err != nil {
		log.Error(ctx, err.Error())
		return rightsList
	}

	list := base.Rows2MapList(ctx, rows)
	if len(list) < 1 {
		return rightsList
	}

	for _, m := range list {
		rights := AdminRightsMap2Model(m)
		rightsList = append(rightsList, rights)
	}

	return rightsList
}

/**restful新增数据
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminRightsCreate(ctx *gin.Context, info map[string]interface{}) uint64 {
	id := base.NewCommonModel(ctx).Create(TABLE_ADMIN_RIGHTS, info)
	return uint64(id)
}

/**restful删除，数据库表中必须有id字段
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminRightsDelete(ctx *gin.Context, id uint64) int64 {
	return base.NewCommonModel(ctx).Delete(TABLE_ADMIN_RIGHTS, helper.ToString(id))
}

/**restful更新(带乐观锁)，数据库表中必须有id,version字段
uint64uint64uint64 * @email jhq0113@163.com
 * generated by gii
*/
func AdminRightsUpdate(ctx *gin.Context, id uint64, version uint64, set map[string]interface{}) int64 {
	return base.NewCommonModel(ctx).UpdateWithLock(TABLE_ADMIN_RIGHTS, helper.ToString(id), version, set)
}

/**restful禁用启用，数据库表中必须有is_on字段
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminRightsUpOrDown(ctx *gin.Context, id uint64, value uint8) int64 {
	return base.NewCommonModel(ctx).UpOrDown(TABLE_ADMIN_RIGHTS, helper.ToString(id), value)
}

/**restful详情，数据库表中必须有id字段
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminRightsInfo(ctx *gin.Context, where map[string]interface{}) AdminRights {
	info := base.NewCommonModel(ctx).Info(TABLE_ADMIN_RIGHTS, where)
	if info != nil {
		return AdminRightsMap2Model(info)
	}
	return AdminRights{}
}

/**restful列表分页检索
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminRightsIndex(ctx *gin.Context, page int64, pageSize int64, where map[string]interface{}, orders []string) map[string]interface{} {
	result := base.NewCommonModel(ctx).Index(TABLE_ADMIN_RIGHTS, page, pageSize, where, orders)

	if len(result["list"].([]map[string]string)) > 0 {
		list := []AdminRights{}
		for _, data := range result["list"].([]map[string]string) {
			list = append(list, AdminRightsMap2Model(data))
		}
		result["list"] = list
	}

	return result
}
